<%@tag pageEncoding="UTF-8"%>
<%@ attribute name="page" type="com.sdgakj.util.Page" required="true"%>
<%@ attribute name="paginationSize" type="java.lang.Integer" required="true"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%
    int current =  page.getPageNo();
//int current =  page.getPageNo();
    int begin = Math.max(1, current - paginationSize/2);
    int end = Math.min(begin + (paginationSize - 1), page.getTotalPage());

    request.setAttribute("current", current);
    request.setAttribute("begin", begin);
    request.setAttribute("end", end);
    String word = (String)request.getAttribute("word");
    request.setAttribute("word", word);
%>

<div style="float:right">
    <ul class="pagination">
        <% if (page.hasPreviousPage()){%>
        <li><a href="?page=1&word=${word}&roleType=${roleType}&sortType=${sortType}&${searchParams}">&lt;&lt;</a></li>
        <li><a href="?page=${current-1}&word=${word}&roleType=${roleType}&sortType=${sortType}&${searchParams}">&lt;</a></li>
        <%}else{%>
        <li class="disabled"><a href="#">&lt;&lt;</a></li>
        <li class="disabled"><a href="#">&lt;</a></li>
        <%} %>

        <c:forEach var="i" begin="${begin}" end="${end}">
            <c:choose>
                <c:when test="${i == current}">
                    <li class="active"><a href="?page=${i}&word=${word}&roleType=${roleType}&sortType=${sortType}&${searchParams}">${i}</a></li>
                </c:when>
                <c:otherwise>
                    <li><a href="?page=${i}&word=${word}&roleType=${roleType}&sortType=${sortType}&${searchParams}">${i}</a></li>
                </c:otherwise>
            </c:choose>
        </c:forEach>

        <% if (page.hasNextPage()){%>
        <li><a href="?page=${current + 1}&word=${word}&roleType=${roleType}&sortType=${sortType}&${searchParams}">&gt;</a></li>
        <li><a href="?page=${page.totalPage}&word=${word}&roleType=${roleType}&sortType=${sortType}&${searchParams}">&gt;&gt;</a></li>
        <%}else{%>
        <li class="disabled"><a href="#">&gt;</a></li>
        <li class="disabled"><a href="#">&gt;&gt;</a></li>
        <%} %>

    </ul>
    <script type="text/javascript">
        function goToPage() {
            var num = $('#topage').val();
            var totalPages = $('#totalPages').val();
            if(num > totalPages) {
                num =  ${page.totalPage};
            }

            if(num < 1 ) {
                num = 1;
            }

            window.location.href="?page=" + num + "&sortType=${sortType}&${searchParams}";
        }
        function pageTab()
        {
            if(event.keyCode ==13)
            {
                goToPage();
            }
        }

    </script>
</div>